/*
 * @Author: zcwhy 12345678
 * @Date: 2023-11-08 16:47:49
 * @LastEditors: zcwhy 12345678
 * @LastEditTime: 2023-11-13 16:44:20
 * @FilePath: \xxProject\src\components\games\phaser-config.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { Scene } from 'phaser'
import { getAssetURL } from '@/utils/index.ts'
export default class PlayScene extends Scene<Phaser.Scene> {
  // showMsg: Phaser.GameObjects.Text;
  add: any;
  load: any;
  make: any;
  physics: any;
  anims: any;
  game: any;
  healthBar: any;
  health: any;
  text: any;
  textMsg: any;
  health2: any;
  text2: any;
  text3: any;
  text4: any;
  textMsg2: any;
  tweens: any;
  damageText: any;
  scoreText:any;
  constructor(game: Phaser.Game) {
    super({ key: 'PlayScene' });
  }
  create() {
    // 添加精灵  
    this.addSprite('myImage', this.game.config.width / 2, 600);
    this.addSprite('monster', this.game.config.width / 2, 200);
    // 创建血条  
    this.addHealthBar(this.game.config.width / 2, 30, this.game.config.width - 40, 10);
    this.addHealthBar(this.game.config.width / 2, 430, this.game.config.width - 40, 10);
    // 添加文本
    this.addText('HP', 20, 5, '#666', '14px');
    this.addText('HP', 20, 405, '#666', '14px');
    this.addText('400/600', this.game.config.width - 70, 5, '#fff', '14px');
    this.addText('400/800', this.game.config.width - 70, 405, '#fff', '14px');
    this.addText('雪狐', 100, 87, '#fff', '18px');
    this.addText('冒险家', 100, 485, '#fff', '18px');
    this.health = 100;
this.scoreText = this.add.text(this.game.config.width / 2, 40, 'score: 0', { fontSize: '20px', fill: 'red' });
    this.scoreText.setText(-100); // 设置文字内容

  }


  addFlameSkill() {
    // 添加火焰技能精灵      
    let flameSprite = this.add.sprite(this.game.config.width / 2, 500, 'flame');

    // 创建火焰技能动画      
    this.anims.create({
      key: 'flameAnimation', // 火焰技能动画的唯一标识符      
      frames: this.anims.generateFrameNumbers('flame', { start: 0, end: 19 }), // 火焰动画的帧数需要根据实际情况修改      
      frameRate: 20, // 火焰动画的帧率可以根据实际情况修改      
      repeat: -1, // 火焰动画的循环次数为-1，表示无限循环      
      onUpdate: (sprite: Phaser.GameObjects.Sprite, frame: number) => {
        console.log('123132'); // 这应该在你调用 this.anims.update() 时被调用  
        // 在这里更新火焰精灵的位置    
        sprite.x += 5; // 假设每次更新时向右移动5像素    
        // 如果火焰精灵移动到了窗口的右边，那么将其移到窗口的左边    
        if (sprite.x > this.game.config.width) {
          sprite.x = -flameSprite.width;  // 注意这里应该用 flameSprite 的宽度，而不是 sprite 的宽度  
        }
      }
    });
  }
  preload() {
    let imgUser = getAssetURL('user/5.jpg')
    let imgMonster1 = getAssetURL('monster/item_01.jpg')
    let imgUser3 = getAssetURL('gif/1/jingmaidonghuaD_001.png')//火焰图片
    // 加载图像资源  
    this.load.image('myImage', imgUser);
    this.load.image('monster', imgMonster1);
    this.load.image('flame', imgUser3);
  }
  addSprite(key: any, x: any, y: any) {
    this.add.sprite(x, y, key).setScale(0.5);
  }
  addHealthBar(x: any, y: any, width: any, height: any) {
    var healthBarObject = this.add.rectangle(x, y, width, height, 0xff0000);
    healthBarObject.health = 1.0;
    healthBarObject.setFillStyle(0xff0000);
    healthBarObject.setStrokeStyle(5, 0xFFFFFF);
  }
  addText(text: any, x: any, y: any, color: any, fontSize: any) {
    this.add.text(x, y, text, { fontFamily: 'Arial', fontSize: fontSize, color: color }); // 根据需要添加颜色  
  }
  update() {
    this.scoreText.setText(-100); // 设置文字内容
  }
}